<?php

namespace App\Listeners;


use App\Events\EnterCar;
use App\Models\EnterCarFlow;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;

/**
 * 进车流统计
 *
 * Class EnterCarFlowUpdate
 * @package App\Listeners
 */
class EnterCarFlowUpdate implements ShouldQueue
{

    public function handle(EnterCar $event)
    {
        $park_code = $event->data->park_code;
        $dateTime = Carbon::createFromTimestamp($event->data->in_time);
        $hour = Carbon::create($dateTime->year, $dateTime->month, $dateTime->day, $dateTime->hour);


        $flowCollection = EnterCarFlow::firstOrNew(
            [
                'park_code' => $park_code,
                'hour' => $hour
            ],
            [
                'flow' => 0
            ]
        );

        $flowCollection->flow += 1;

        \Log::info('进车记录汇聚');

        $flowCollection->save();
    }
}
